import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:shenling_project/utils/toast.dart';
import 'package:shenling_project/utils/tokenManager.dart';

class Settingpage extends StatefulWidget {
  const Settingpage({super.key});

  @override
  State<Settingpage> createState() => _SettingpageState();
}

class _SettingpageState extends State<Settingpage> {
  final List menuData = [
    //文字和右箭头图标
    {'title': '换绑手机', 'icon': 'assets/images/right.png'},
    {'title': '修改密码', 'icon': 'assets/images/right.png'},
    {'title': '消息通知设置', 'icon': 'assets/images/right.png'},
    {'title': '清理缓存', 'icon': 'assets/images/right.png'},
  ];
//菜单builder
  Widget menuBuilder(item) {
    return SizedBox(
      height: 40,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Text(item['title']),
          Image.asset(
            item['icon'],
            width: 24,
            height: 24,
            color: const Color.fromARGB(42, 26, 26, 25),
          )
        ],
      ),
    );
  }

//退出登录
  _logout() {
    return showDialog(
        context: context,
        builder: (ctx) {
          return AlertDialog(
            title: const Text('温馨提示'),
            content: const Text('您确定要退出登录吗?'),
            actions: [
              TextButton(
                  onPressed: () {
                    Navigator.pop(context);
                  },
                  child: const Text('取消')),
              TextButton(
                  onPressed: () {
                    TokenManager().removeToken();
                    Navigator.pushNamed(context, '/login');
                    ToastUtil.showSuccess('退出成功!');
                  },
                  child: const Text('确定')),
            ],
          );
        });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        // 设置导航条
        appBar: AppBar(
          title: const Text('系统设置',
              style: TextStyle(color: Colors.black, fontSize: 20)),
          backgroundColor: Colors.transparent,
          centerTitle: true,
          //左侧图标
          leading: IconButton(
            icon: const Icon(Icons.arrow_back_ios),
            onPressed: () {
              Navigator.pop(context);
            },
          ),
        ),
        // 设置主体内容
        body: Container(
          padding: const EdgeInsets.all(10),
          child: Column(
            children: [
              Container(
                width: MediaQuery.of(context).size.width,
                height: 250,
                margin: const EdgeInsets.only(left: 16, right: 16),
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(10),
                  color: Colors.white,
                ),
                child: ListView.separated(
                    padding: EdgeInsets.zero,
                    physics: const ClampingScrollPhysics(),
                    shrinkWrap: true,
                    itemBuilder: (ctx, index) {
                      return Container(
                        padding: const EdgeInsets.all(10),
                        child: menuBuilder(menuData[index]),
                      );
                    },
                    separatorBuilder: (ctx, index) {
                      return const Divider(
                        height: 1,
                        indent: 12,
                        endIndent: 12,
                      );
                    },
                    itemCount: menuData.length),
              ),
              const SizedBox(
                height: 20,
              ),
              SizedBox(
                width: MediaQuery.of(context).size.width - 50,
                height: 60,
                child: ElevatedButton(
                    //取出按钮默认的边框圆角
                    style: ButtonStyle(
                      shape: WidgetStateProperty.all(RoundedRectangleBorder(
                          borderRadius: BorderRadius.circular(10))),
                    ),
                    onPressed: () {
                      _logout();
                    },
                    child: const Text(
                      '退出',
                      style: TextStyle(
                          color: Color.fromRGBO(42, 41, 41, 1), fontSize: 18),
                    )),
              )
            ],
          ),
        ));
  }
}
